﻿// This software is the confidential and proprietary information of NetBay.
// Such confidential information should not be disclosed.
// NetBay copyright(c) 2012

#region

using System;
using System.IO;
using System.Security;
using System.Web;
using NetBay.Core.Encryption.Licensing;
using Rio.Applications.Web.LicenseFiles;
using Rio.Framework.RioManager;

#endregion

namespace Rio.Applications.Web.Code
{
    public class LicenseManager
    {
        #region Class Properties

        public static bool IsValidLicense
        {
            get
            {
                if (LicenseTerms != null)
                    return true;
                return false;
            }
        }

        /// <summary>
        /// Gets the terms.
        /// </summary>
        public static LicenseTerms LicenseTerms
        {
            get
            {
                if (HttpContext.Current.Application["LicenseTerms"] != null)
                {
                    return (LicenseTerms) HttpContext.Current.Application["LicenseTerms"];
                }
                return null;
            }
        }

        #endregion

        #region Class Methods

        /// <summary>
        /// Registers the license.
        /// </summary>
        public static void RegisterLicense()
        {
            //Checking if licence file is present or not.
            if (IsValidLicense)
                return;

            //License validation
            try
            {
                LicenseAuthorization licenseAuthorization = new LicenseAuthorization();
                LicenseTerms terms =
                    licenseAuthorization.ValidateLicense(Path.Combine(HttpContext.Current.Server.MapPath("bin"),
                                                                      "RioWeb.lic"));
                HttpContext.Current.Application["LicenseTerms"] = terms;
            }
            catch (SecurityException ex)
            {
                LoggerManager.GetBusinessLogger.LogError("Unable to load license file : " + ex.Message);
                throw;
            }
            catch (Exception ex)
            {
                LoggerManager.GetBusinessLogger.LogError("Unexpected exception in load license file : " + ex.Message);
                throw;
            }
        }

        #endregion
    }
}